<html lang="en">
<head>
    <meta charset="utf-8">
    <title>分支管理</title>
    <link href="/static/css/start/jquery-ui-1.10.4.custom.css" rel="stylesheet">
    <link href="/static/css/box.css" rel="stylesheet">
    <link href="/static/css/table.css" rel="stylesheet">
    <script src="/static/js/jquery-1.10.2.js"></script>
    <script src="/static/js/jquery-ui-1.10.4.custom.js"></script>
    <script src="/static/js/helper.js"></script>
    <script>
        var branchOnEdit=false;
        var branchesData=[];
        function branchChangeToEdit(edit,index)
        {
            branchOnEdit=edit;
            if(edit)
            {
                $('#editBranchOK').show();
                $('#editBranchCancel').show();
                $('#addBranch').hide();
                $('#list').hide();
                var id=branchesData[index][0];
                var name=branchesData[index][1];
                var desc=branchesData[index][2];
                $('#addBranchName').val(name);
                $('#addBranchDescription').val(desc);
                $('#branchID').val(id);
            }
            else
            {
                $('#editBranchOK').hide();
                $('#editBranchCancel').hide();
                $('#addBranch').show();
                $('#list').show();
                $('#addBranchName').val('');
                $('#addBranchDescription').val('');
                $('#branchID').val('');
            }
        }
        function delBranch(id)
        {
            callback=function(){
                $.post('delBranches.py',{id:id},
                        function(result,status){
                            if(status == 'success'){
                                if(result.success){
                                    showMessage('删除成功')
                                    loadBranches();
                                }else{
                                    showMessage(result.message)
                                }
                            }
                        });
            }
            deleteConfim(callback);
        }
        function loadBranches(){

            $.post('getBranches.py',{keyword:$('#branchNameKey').val()},
                    function(result,status){
                        if(status == 'success'){
                            if(result.success){
                                html=[];
                                branchesData=result.result;
                                for(i in result.result)
                                {
                                    html.push('<tr onmousemove="highlight_line(this)" onmouseout="clear_highlight_line(this)">');
                                    html.push('<td>');
                                    html.push(result.result[i][1]);
                                    html.push('</td>');
                                    html.push('<td>');
                                    html.push(htmlEncode(result.result[i][2]));
                                    html.push('</td>');

                                    html.push('<td class="nowrap">');
                                    html.push("<a href='#' onclick='branchChangeToEdit(true,");
                                    html.push(i);
                                    html.push(")'>编辑</a>");
                                    html.push('&nbsp;&nbsp;');
                                    html.push("<a href='#' onclick='delBranch(");
                                    html.push(result.result[i][0]);
                                    html.push(")'>删除</a>");

                                    html.push('</td>');
                                    html.push('</tr>');
                                }

                                $('#branchesData').html(html.join(''));
                            }else{
                            }
                        }
                    });


        }
        function saveBranch()
        {
            if(checkUnique('addBranchName')){
                $.post('saveBranch.py',{id:$("#branchID").val(),name:$("#addBranchName").val(),description:$("#addBranchDescription").val()},
                        function(result,status){
                            if(status == 'success'){
                                if(result.success){
                                    if(branchOnEdit)
                                    {
                                        branchChangeToEdit(false,0);
                                        showMessage("修改成功");

                                    }
                                    else
                                    {
                                        showMessage("添加成功");
                                    }
                                    $("#addBranchName").val('')
                                    $("#addBranchDescription").val('')
                                    loadBranches();
                                }else{
                                    showMessage(result.message);
                                }
                            }
                        });
            }
        }
        function enterKeyLoadBranches(event)
        {
            if(event.which==13){
                loadBranches();
            }
        }
        $(function() {
            $('.mybutton').button();
            branchChangeToEdit(false,0);
            loadBranches();
            $("#branchNameKey").keydown(enterKeyLoadBranches);
        });
    </script>
</head>
<body>
<h1 style="text-align: center" class="ui-dialog-titlebar">分支管理</h1>
<hr>

<input type="hidden" id="branchID">
<table border="0" style="margin: auto">
    <tr>
        <td><label for="addBranchName">分支名称</label></td>
        <td><input type="text" style="width: 250px" id="addBranchName"></td>
        <td><label id="addBranchNameMsg" class="ui-state-error ui-helper-hidden"></label></td>
    </tr>
    <tr>
        <td><label for="addBranchDescription" >分支说明</label></td>
        <td><textarea id="addBranchDescription" style="width: 250px;height: 100px"></textarea></td>
        <td>&nbsp;&nbsp;&nbsp;</td>
    </tr>
    <tr>
        <td colspan="3" style="text-align: right">
            <button class="mybutton" onclick="saveBranch();" id="editBranchOK">修改</button>
            <button class="mybutton" onclick="branchChangeToEdit(false,0);" id="editBranchCancel">取消</button>
            <button class="mybutton" onclick="saveBranch();" id="addBranch">添加</button>
        </td>
    </tr>
</table>

<div id="list" style="text-align: center">
    <hr>
    <input type="text" id="branchNameKey" style="width: 60%;"><button class="mybutton" onclick="loadBranches();" style="font-size: 13px;">查找</button>
    <table border="1" style="width: 100%" >
        <thead>
        <th>分支名</th>
        <th>分支说明</th>
        <th>操作</th>
        </thead>
        <tbody id="branchesData" ></tbody>
    </table>
</div>

</body>
</html>